package org.example.aienglishapp.controller.word;

import org.example.aienglishapp.entity.word.Word;
import org.example.aienglishapp.service.word.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/words")
@CrossOrigin
public class WordController {

    @Autowired
    private WordService wordService;
    private JdbcTemplate jdbcTemplate;

    public WordController(WordService wordService) {
        this.wordService = wordService;
    }


    @GetMapping("/by-book/{wordBook}")
    public List<Word> findWordsByWordBook(@PathVariable String wordBook) {
        return wordService.getWordsByWordBook(wordBook);
    }

    @GetMapping("/with-explanation/{word}")
    public Optional<Word> getWordWithExplanation(@PathVariable String word) {
        return wordService.getWordWithExplanation(word);
    }

    @GetMapping("/meaning/{word}")
    public String getChineseMeaning(@PathVariable String word) {
        return wordService.getChineseMeaning(word);
    }

    @GetMapping("/words/random/from/{book}")
    public ResponseEntity<?> getRandomWordFromBook(@PathVariable String book) {
        return wordService.getRandomWordFromBook(book);
    }


    @PostMapping("/words/add-to-wrong-words")
    public ResponseEntity<?> addWordToWrongWords(@RequestBody Map<String, String> requestBody) {
        String word = requestBody.get("word");
        String book = requestBody.get("book");
        return wordService.addWordToWrongWords(word, book);

    }


    @GetMapping
    public ResponseEntity<List<Word>> getWordsByDifficulty(@RequestParam String difficulty) {
        System.out.println("Received difficulty: " + difficulty);
        List<Word> words = wordService.getWordsByDifficulty(difficulty);
        System.out.println("Found words: " + words);
        return ResponseEntity.ok(words);
    }

    @GetMapping("/wordbooks")
    public ResponseEntity<List<String>> getAllWordBooks() {
        System.out.println("Received request to get all wordbooks");
        List<String> wordbooks = wordService.getAllWordBooks();
        System.out.println("Found wordbooks: " + wordbooks);
        return ResponseEntity.ok(wordbooks);
    }

}